﻿namespace RPCenter.WindowsAPI
{
    using Microsoft.VisualBasic.CompilerServices; 
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Runtime.CompilerServices;
    using System.Runtime.InteropServices;

    public class NativeLogonSession
    {
        [CompilerGenerated]
        private int _SessionId;
        [CompilerGenerated]
        private string _Username;
        [CompilerGenerated]
        private string _WinStation;

        public NativeLogonSession()
        {
            this.Username = string.Empty;
            this.WinStation = string.Empty;
        }

        public static List<NativeLogonSession> GetLogonSessions(bool IncludeServicesSession)
        {
            List<NativeLogonSession> list2 = new List<NativeLogonSession>();
            IntPtr zero = IntPtr.Zero;
            uint pCount = 0;
            if (!ApiDefinitions.WTSEnumerateSessions(IntPtr.Zero, 0, 1, ref zero, ref pCount))
            {
                throw new Win32Exception();
            }
            if (pCount > 0L)
            {
                int num2 = Marshal.SizeOf(typeof(ApiDefinitions.WTS_SESSION_INFO));
                int num4 = (int) (pCount - 1L);
                for (int i = 0; i <= num4; i++)
                {
                    try
                    {
                        ApiDefinitions.WTS_SESSION_INFO wts_session_info = (ApiDefinitions.WTS_SESSION_INFO) Marshal.PtrToStructure(zero, typeof(ApiDefinitions.WTS_SESSION_INFO));
                        if (IncludeServicesSession || (string.Compare(wts_session_info.pWinStationName, "services", true) != 0))
                        {
                            NativeLogonSession session2 = new NativeLogonSession {
                                WinStation = wts_session_info.pWinStationName,
                                SessionId = (int) wts_session_info.SessionId
                            };
                            NativeLogonSession item = session2;
                            item.Username = GetUsernameFromSessionId((int) wts_session_info.SessionId);
                            list2.Add(item);
                        }
                    }
                    finally
                    {
                        try
                        {
                            ApiDefinitions.WTSFreeMemory(zero);
                        }
                        catch (Exception exception1)
                        {
                            ProjectData.SetProjectError(exception1);
                            Exception exception = exception1;
                            ProjectData.ClearProjectError();
                        }
                        zero = new IntPtr(zero.ToInt64() + num2);
                    }
                }
            }
            return list2;
        }

        public static string GetUsernameFromSessionId(int SessionId)
        {
            string str;
            IntPtr zero = IntPtr.Zero;
            IntPtr ppBuffer = IntPtr.Zero;
            try
            {
                string str3 = string.Empty;
                string str2 = string.Empty;
                uint pBytesReturned = 0;
                if ((ApiDefinitions.WTSQuerySessionInformation(IntPtr.Zero, (uint) SessionId, ApiDefinitions.WTS_INFO_CLASS.WTSUserName, ref zero, ref pBytesReturned) && !(zero == IntPtr.Zero)) && (pBytesReturned > 0L))
                {
                    str3 = Marshal.PtrToStringUni(zero);
                }
                uint num = 0;
                if ((ApiDefinitions.WTSQuerySessionInformation(IntPtr.Zero, (uint) SessionId, ApiDefinitions.WTS_INFO_CLASS.WTSDomainName, ref ppBuffer, ref num) && !(ppBuffer == IntPtr.Zero)) && (num > 0L))
                {
                    str2 = Marshal.PtrToStringUni(ppBuffer);
                }
                if (string.IsNullOrEmpty(str2))
                {
                    return str3;
                }
                str = str2 + @"\" + str3;
            }
            catch (Exception exception1)
            {
                ProjectData.SetProjectError(exception1);
                Exception exception = exception1;
                throw new ApplicationException("Error getting username and domain name from session ID " + Conversions.ToString(SessionId) + ". The last error reported was: " + new Win32Exception().Message);
            }
            finally
            {
                if (!(zero == IntPtr.Zero))
                {
                    ApiDefinitions.WTSFreeMemory(zero);
                }
                if (!(ppBuffer == IntPtr.Zero))
                {
                    ApiDefinitions.WTSFreeMemory(ppBuffer);
                }
            }
            return str;
        }

        public int SessionId
        {
            get
            {
                return this._SessionId;
            }
            set
            {
                this._SessionId = value;
            }
        }

        public string Username
        {
            get
            {
                return this._Username;
            }
            set
            {
                this._Username = value;
            }
        }

        public string WinStation
        {
            get
            {
                return this._WinStation;
            }
            set
            {
                this._WinStation = value;
            }
        }
    }
}

